package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Notice;
import cn.wolfcode.crm.query.JsonResult;
import cn.wolfcode.crm.query.NoticeQueryObject;
import cn.wolfcode.crm.service.INoticeService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequiresPermissions(value = {"notice:list","公告查看页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo){
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("result", pageInfo);
        return "notice/list";
    }

    // 跳转到查看页面
    @RequestMapping("/show")
    public String show(Model model, Long id){
        model.addAttribute("noticeList",noticeService.get(id));
        noticeService.saveNoticeEmployee(id);
        return "notice/show";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"notice:delete","公告查看删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            noticeService.delete(id);
        }
        return new JsonResult(true,"删除成功");
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"notice:saveOrUpdate","公告查看新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Notice notice){
        if (notice.getId() != null) {
            noticeService.update(notice);
            return new JsonResult(true,"修改成功");
        }else {
            noticeService.save(notice);
        }
        return new JsonResult(true,"添加成功");
    }
}
